{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://mp.weixin.qq.com/s?__biz=MzkxODE3NjExOQ==&mid=2247485433&idx=1&sn=e54be50e187c1fa2bf8041b51bf0a2a4&chksm=c1b42fe1f6c3a6f788d28c35c7ab725716b2bb1c76154af8d5dae35f19859a5cdd2410526009&scene=178&cur_album_id=3354758663365918722#rd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "索提诺比率（Sortino Ratio）是一种风险调整收益率的指标，用于评估投资策略、基金或投资组合在考虑下行风险的情况下的表现。它与夏普比率类似，但区别在于索提诺比率仅考虑了投资回报下跌的风险，即下行风险，而不是总体波动性。\n",
    "索提诺比率的计算公式如下：\n",
    "索提诺比率 =（投资组合收益率- 无风险利率）/ 下行标准差\n",
    "其中，下行标准差是指投资回报低于某个给定目标或最低期望回报率时回报率的波动性。这个目标通常设为无风险利率，但也可以是零收益或投资者的最低期望回报。\n",
    "索提诺比率的核心优势在于它只惩罚那些投资者不希望承担的风险——亏损的风险。这与夏普比率不同，后者将所有的波动性都视为风险，无论是向上还是向下。因此，如果一个投资组合或策略的收益率分布是非对称的，特别是在收益率倾向于在正面有大的波动时，索提诺比率可能是一个更合适的性能衡量标准。\n",
    "在实际应用中，索提诺比率可以帮助投资者识别和选择那些在相同的下行风险水平下能够提供更高超额回报的投资组合。高索提诺比率意味着在承受较低的不利波动性（或损失风险）的情况下，投资策略或基金的性能更好。这使得索提诺比率成为评估投资风险-收益特性的重要工具，尤其是对于那些对损失更为敏感的投资者。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 导入需要使用的库\n",
    "import akshare as ak\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 关闭警告信息\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "# 获取沪深300指数10年的收盘价数据\n",
    "start_date = '20140101'  # 开始日期\n",
    "end_date = '20231229'  # 结束日期\n",
    "bars = ak.stock_zh_index_hist_csindex(symbol='000300', start_date=start_date, end_date=end_date)\n",
    "prices = bars[['日期','收盘']]\n",
    "# 将日期设置为datetime格式\n",
    "prices['日期'] = pd.to_datetime(prices['日期'])\n",
    "prices = prices.set_index('日期')\n",
    "# 计算日收益率\n",
    "returns = prices['收盘'].pct_change()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 索提诺比率：使用简单年化收益率\n",
    "rf = 0.0  # 假设无风险利率为0\n",
    "annual_periods = 244  # 年化期数，一年约244个交易日\n",
    "n = len(returns)\n",
    "\n",
    "# 计算超额收益\n",
    "returns = returns - rf\n",
    "\n",
    "# 计算下行差异：如果returns的值小于0则保留，大于0的值则设为0\n",
    "downside_returns = np.clip(returns, -np.inf, 0)\n",
    "# 计算年化下行波动率\n",
    "downside_variance = np.sum(downside_returns ** 2) / n\n",
    "downside_deviation = np.sqrt(downside_variance * 244)\n",
    "\n",
    "# 简单年化收益率 = 投资组合的平均超额收益率 * 年化期数\n",
    "return_mean = np.mean(returns) * annual_periods\n",
    "\n",
    "# 索提诺比率 = 简单年化收益率 / 年化下行波动率\n",
    "sortino = return_mean / downside_deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 索提诺比率：使用复合年化收益率\n",
    "# 复合年化收益\n",
    "years = n / annual_periods  # 投资年数\n",
    "total = np.prod(1 + returns) - 1  # 总收益率\n",
    "annualized_return = abs(total + 1.0) ** (1.0 / years) - 1  # 复合年化收益率\n",
    "\n",
    "# 索提诺比率 = 复合年化收益率 / 年化下行波动率\n",
    "sortino = annualized_return / downside_deviation"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "4broker",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
